#include <iostream>

#include "database/simple_db_pool.h"

namespace base {

SimpleConnectionPool::~SimpleConnectionPool() {
  clear();
}

mysqlpp::Connection* SimpleConnectionPool::grab() {
  return mysqlpp::ConnectionPool::grab();
}

void SimpleConnectionPool::release(const mysqlpp::Connection* pc) {
  mysqlpp::ConnectionPool::release(pc);
}

mysqlpp::Connection* SimpleConnectionPool::create() {
  std::cout << "new conn created." << std::endl;
  return new mysqlpp::Connection(
      db_.empty() ? 0 : db_.c_str(),
      server_.empty() ? 0 : server_.c_str(),
      user_.empty() ? 0 : user_.c_str(),
      password_.empty() ? "" : password_.c_str());
}

void SimpleConnectionPool::destroy(mysqlpp::Connection* cp) {
  // Our superclass can't know how we created the Connection, so
  // it delegates destruction to us, to be safe.
  delete cp;
}

unsigned int SimpleConnectionPool::max_idle_time()
{
  // Set our idle time at an example-friendly 3 seconds.  A real
  // pool would return some fraction of the server's connection
  // idle timeout instead.
  return 300;
}

}

